package com.example.dataservice.mapper;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.example.api.model.RechargeRecord;
import org.example.api.model.User;
import org.example.api.pojo.IncomeInfo;
import org.example.api.pojo.UserBidInfo;
import org.example.api.pojo.UserCenterInfo;

import java.util.List;

public interface UserMapper {

    @Select("""
            select u_user.id, phone, name, last_login_time ,available_money
            from u_user join u_finance_account
            on u_user.id = u_finance_account.uid
            where u_user.id = #{id}
            """)
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "phone", property = "phone"),
            @Result(column = "name", property = "name"),
            @Result(column = "last_login_time", property = "lastLoginTime"),
            @Result(column = "available_money", property = "availableMoney"),
            @Result(column = "id", property = "bidInfos",
                    many = @Many(select = "selectUserBidInfo", fetchType = FetchType.LAZY)),
            @Result(column = "id", property = "rechargeRecords",
                    many = @Many(select = "selectUserCharge", fetchType = FetchType.LAZY)),
            @Result(column = "id", property = "incomeInfos",
                    many = @Many(select = "selectUserIncome", fetchType = FetchType.LAZY)),
    })
    /*1.用户中心，查询用户的基本信息*/
    UserCenterInfo selectUserCenterInfo(Integer id);
    @Select("""
            select b.id,p.product_name,b.bid_money,b.bid_time 
            from b_bid_info b join b_product_info p 
            on b.prod_id = p.id
            where uid = #{id}
            order by bid_time desc limit 0, 5;
            """)
    @ResultType(UserBidInfo.class)
    /*2.用户中心，投资记录*/
    List<UserBidInfo> selectUserBidInfo(Integer id);
    @Select("""
            select * from b_recharge_record 
            where uid = #{id} and recharge_status = 1
            order by recharge_time desc limit 0, 5;
            """)
    @ResultType(RechargeRecord.class)
    /*3.用户中心，充值记录*/
    List<RechargeRecord> selectUserCharge(Integer id);
    @Select("""
            select i.id, p.product_name, i.income_money, i.income_date
            from b_income_record i join b_product_info p
            on i.prod_id = p.id 
            where i.income_status = 1 and i.uid = #{id}
            order by i.income_date desc limit 0, 5;
            """)
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "product_name", property = "productName"),
            @Result(column = "income_money", property = "incomeMoney"),
            @Result(column = "income_date", property = "incomeDate"),
    })
    /*4. 用户的最近收益*/
    List<IncomeInfo> selectUserIncome(Integer id);


    @Update("""
            update u_use          r set name = #{name}, id_card = #{idCard} where phone = #{phone};
            """)
    /*用户实名认证成功后，根据phone更新用户的实名认信息*/
    Integer updateUserInfo(@Param("phone") String phone, @Param("name") String name, @Param("idCard") String idCard);


    @Select("""
            select * from u_user where phone = #{phone} and login_password = #{loginPassword}
            """)
    @ResultType(User.class)
    /*根据用户手机号和密码进行用户查询*/
    User selectUserLogin(@Param("phone") String phone, @Param("loginPassword") String password);


    @Insert("""
            insert into u_user(phone,login_password,add_time)
            values(#{phone}, #{loginPassword}, #{addTime})
            """)
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    /*注册用户，获取自增主键*/
    int insertRegisterUser(User user);

    @Select("""
            select phone from u_user where phone = #{phone};
            """)
    @ResultType(User.class)
    /*通过手机号查询是否存在用户*/
    User selectByPhone(@Param("phone") String phone);


    @Select("""
            select * from u_user;
            """)
    @ResultType(User.class)
    /*查询所有的用户*/
    List<User> selectAllUser();

    @Select("""
            select count(id) as ct from u_user;
            """)
    @ResultType(Integer.class)
    /*统计注册人数*/
    int selectCountUser();

    int deleteByPrimaryKey(Integer id);

    int insert(User record);

    int insertSelective(User record);

    User selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);


}